import itertools


def is_prime(num):
    if num < 2:
        return False
    for i in range(2, int(num**0.5) + 1):
        if num % i == 0:
            return False
    return True

def find_largest_prime(input_number):
    if len(input_number) != 4:
        return 0

    digits = [int(d) for d in input_number]
    digits.sort(reverse=True)

    for i in range(4, 0, -1):
        for combo in itertools.permutations(digits, i):
            num = int(''.join(map(str, combo)))
            if is_prime(num):
                return num

    return 0

input_number = input("Введите четырёхзначное натуральное число: ")
result = find_largest_prime(input_number)

print(result)
